(function() {
  var $el = $('.element-editor[data-element-id="<%= @element.id %>"]');

<% if @error -%>

  $("#element_<%= @element.id %> .spinner")
    .replaceWith("<span class='error_icon' title='<%= @error %>'>!</span>");

<% else -%>

  $el.replaceWith('<%= j render(partial: "element", object: @element) %>');
  $el = $('#element_<%= @element.id %>');
  $('#element_area .sortable_cell').sortable('refresh');

  <% if @element.folded? -%>

  Alchemy.Tinymce.remove(<%= @element.richtext_contents_ids.to_json %>);

  <% else -%>

  $el.trigger('FocusElementEditor.Alchemy');
  Alchemy.Tinymce.init(<%= @element.richtext_contents_ids.to_json %>);
  Alchemy.GUI.initElement($el);
  Alchemy.SortableElements(
    <%= @page.id %>,
    '<%= form_authenticity_token %>',
    $('> .nestable-elements .nested-elements', $el)
  );

  <% end -%>

<% end -%>
})();
